package com.tju.resource.business.tb_matl_val.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.business.tb_matl_val.TbMatlValService;
import com.tju.resource.mapper.TbMatlValMapper;
import com.tju.resource.model.TbMatlVal;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.EmptyUtils;
import com.tju.resource.tool.tool.QuickQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 */
@Service
public class TbMatlValServiceImpl extends ServiceImpl<TbMatlValMapper, TbMatlVal>
        implements TbMatlValService {

    @Autowired
    TbMatlValMapper mapper;

    @Override
    public Result<Object> getAll(JSONObject params) {
        try {
            Integer pageNum = params.getInt("page");
            Integer limit = params.getInt("limit");
            if (EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(limit)) {
                Page<TbMatlVal> page = new Page<>(pageNum, limit);
                IPage<TbMatlVal> iPage = mapper.getAll(page, params);
                return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    @Override
    public Result<Object> upd(TbMatlVal matlVal) {
        matlVal.setUpdateTime(new Date());

        int update = mapper.updateById(matlVal);

        if (update > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, "更新成功!");
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "更新失败!");
        }
    }

    @Override
    public Result<Object> add(TbMatlVal matlVal) {
        matlVal.setCreateTime(new Date());

        int update;

        if (null != matlVal.getMid()) {
            update = mapper.insert(matlVal);
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "物料必选!");
        }

        if (null == matlVal.getVal()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "单件值不可为空!");
        }

        if (update > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, "新增成功!");
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "新增失败!");
        }
    }

    @Override
    public Result<Object> del(TbMatlVal matlVal) {
        int update = mapper.deleteById(matlVal);

        if (update > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, "删除成功!");
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "删除失败!");
        }
    }

    @Override
    public Result<Object> getVal(Long mid) {
        try {
            Result<Object> success = ResultUtil.success(ResultEnum.SUCCESS, "");
            TbMatlVal val = QuickQuery.byOne(mapper, "mid", mid);

            success.setData(val);
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "Query Failed: " + e.getMessage());
        }
    }
}




